"""
Built-in validate comparators.
"""

import re
from typing import Text, Any, Union

from loguru import logger


class Assert:
    @staticmethod
    def equal(check_value: Any, expect_value: Any, message: Text = ""):
        logger.info(f"assert：except:{expect_value}  actual:{check_value}")
        assert check_value == expect_value, message

    @staticmethod
    def greater_than(
            check_value: Union[int, float], expect_value: Union[int, float], message: Text = ""
    ):
        logger.info(f"assert：except:{expect_value}  actual:{check_value}")
        assert check_value > expect_value, message

    @staticmethod
    def less_than(
            check_value: Union[int, float], expect_value: Union[int, float], message: Text = ""
    ):
        logger.info(f"assert：except:{expect_value}  actual:{check_value}")
        assert check_value < expect_value, message

    @staticmethod
    def greater_or_equals(
            check_value: Union[int, float], expect_value: Union[int, float], message: Text = ""
    ):
        logger.info(f"assert：except:{expect_value}  actual:{check_value}")
        assert check_value >= expect_value, message

    @staticmethod
    def less_or_equals(
            check_value: Union[int, float], expect_value: Union[int, float], message: Text = ""
    ):
        logger.info(f"assert：except:{expect_value}  actual:{check_value}")
        assert check_value <= expect_value, message

    @staticmethod
    def not_equal(check_value: Any, expect_value: Any, message: Text = ""):
        logger.info(f"assert：except:{expect_value}  actual:{check_value}")
        assert check_value != expect_value, message

    @staticmethod
    def string_equals(check_value: Text, expect_value: Any, message: Text = ""):
        logger.info(f"assert：except:{expect_value}  actual:{check_value}")
        assert str(check_value) == str(expect_value), message

    @staticmethod
    def length_equal(check_value: Text, expect_value: int, message: Text = ""):
        assert isinstance(expect_value, int), "expect_value should be int type"
        logger.info(f"assert：except:{expect_value}  actual:{check_value}")
        assert len(check_value) == expect_value, message

    @staticmethod
    def len_list(check_value: Text, expect_value: list, message: Text = ""):
        assert isinstance(expect_value, Text)
        logger.info(f"assert：except:{expect_value}  actual:{check_value}")
        assert check_value[0] == expect_value

    @staticmethod
    def length_greater_than(
            check_value: Text, expect_value: Union[int, float], message: Text = ""
    ):
        assert isinstance(
            expect_value, (int, float)
        ), "expect_value should be int/float type"
        logger.info(f"assert：except:{expect_value}  actual:{check_value}")
        assert len(check_value) > expect_value, message

    @staticmethod
    def length_greater_or_equals(
            check_value: Text, expect_value: Union[int, float], message: Text = ""
    ):
        assert isinstance(
            expect_value, (int, float)
        ), "expect_value should be int/float type"
        logger.info(f"assert：except:{expect_value}  actual:{check_value}")
        assert len(check_value) >= expect_value, message

    @staticmethod
    def length_less_than(
            check_value: Text, expect_value: Union[int, float], message: Text = ""
    ):
        assert isinstance(
            expect_value, (int, float)
        ), "expect_value should be int/float type"
        logger.info(f"assert：except:{expect_value}  actual:{check_value}")
        assert len(check_value) < expect_value, message

    @staticmethod
    def length_less_or_equals(
            check_value: Text, expect_value: Union[int, float], message: Text = ""
    ):
        assert isinstance(
            expect_value, (int, float)
        ), "expect_value should be int/float type"
        logger.info(f"assert：except:{expect_value}  actual:{check_value}")
        assert len(check_value) <= expect_value, message

    @staticmethod
    def contains(check_value: Any, expect_value: Any, message: Text = ""):
        assert isinstance(
            check_value, (list, tuple, dict, str, bytes)
        ), "expect_value should be list/tuple/dict/str/bytes type"
        logger.info(f"assert：except:{expect_value}  actual:{check_value}")
        assert expect_value in check_value, message

    @staticmethod
    def contained_by(check_value: Any, expect_value: Any, message: Text = ""):
        assert isinstance(
            check_value, (list, tuple, dict, str, bytes)
        ), "expect_value should be list/tuple/dict/str/bytes type"
        logger.info(f"assert：except:{expect_value}  actual:{check_value}")
        assert check_value in expect_value, message

    @staticmethod
    def type_match(check_value: Any, expect_value: Any, message: Text = ""):
        def get_type(name):
            if isinstance(name, type):
                return name
            elif isinstance(name, str):
                try:
                    return __builtins__[name]
                except KeyError:
                    raise ValueError(name)
            else:
                raise ValueError(name)

        if expect_value in ["None", "NoneType", None]:
            logger.info(f"assert：except:{expect_value}  actual:{check_value}")
            assert check_value is None, message
        else:
            logger.info(f"assert：except:{expect_value}  actual:{check_value}")
            assert type(check_value) == get_type(expect_value), message

    @staticmethod
    def regex_match(check_value: Text, expect_value: Any, message: Text = ""):
        assert isinstance(expect_value, str), "expect_value should be Text type"
        assert isinstance(check_value, str), "check_value should be Text type"
        logger.info(f"assert：except:{expect_value}  actual:{check_value}")
        assert re.match(expect_value, check_value), message

    @staticmethod
    def startswith(check_value: Any, expect_value: Any, message: Text = ""):
        logger.info(f"assert：except:{expect_value}  actual:{check_value}")
        assert str(check_value).startswith(str(expect_value)), message

    @staticmethod
    def endswith(check_value: Text, expect_value: Any, message: Text = ""):
        logger.info(f"assert：except:{expect_value}  actual:{check_value}")
        assert str(check_value).endswith(str(expect_value)), message
